<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        placeholder="请输入部门名称"
        v-model="listQuery.department"
        style="width: 200px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
     <el-input
        placeholder="请输入小组名称"
        v-model="listQuery.group"
        style="width: 200px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
    </div>
    <div class="options-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="addCtlShow()"
        >添加</el-button
      >
    </div>
    <div class="table-container">
      <el-table
        v-loading="listLoading"
        :key="tableKey"
        :data="list"
        border
        fit
        @sort-change="sortHanlde"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          prop="id"
          sortable="custom"
          align="left"
          width="105"
        >
        </el-table-column>
        <el-table-column
          label="部门"
          prop="department"
          sortable="custom"
          width="150px"
          align="left"
        >
        </el-table-column>
        <el-table-column
          label="小组"
          prop="group"
          sortable="custom"
          width="150px"
          align="left"
        >
        </el-table-column>      
        <el-table-column
          label="操作"
          align="left"
          width="300"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">          
            <el-button type="success" size="mini" @click="updateDep(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="removeDep(scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="pageHandle"
      />
    </div>
    <div class="dialog-container">
      <!-- 角色编辑弹框 -->
      <departmentEditctl
        :show.sync="departmentEdit.show"
        :data="departmentEdit.data"
        @success="getList"
      >
      </departmentEditctl>

      <departmentAddctl :show.sync="departmentAdd.show" @success="getList">
      </departmentAddctl>
  
    </div>
  </div>
</template>

<script>
import { getlist, remove_by_id } from "@/api/org_department";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import departmentAddctl from "./components/departmentAdd";
import departmentEditctl from "./components/departmentEdit";


export default {
  name: "rolesManage",
  components: { Pagination,departmentAddctl,departmentEditctl},
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        department: "",
        group: "",
        page: 1,
        limit: 20,
        filed: "id",
        sort: "ascending",
      },
      departmentEdit: {
        show: false,
        data: undefined,
      },
      departmentAdd: {
        show: false,
        data: undefined,
      },   
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      getlist(this.listQuery).then((response) => {
        this.list = response.data;
        this.total = response.count || 0;
        setTimeout(() => {
          this.listLoading = false;
        }, 500);
      });
    },
    handleFilter() {
      this.getList();
    },
    sortHanlde(column) {
      console.log(column);
      this.listQuery.filed = column.prop;
      this.listQuery.sort = column.order;
      this.getList();
    },
    pageHandle(page) {
      this.listQuery.page = page.page;
      this.listQuery.limit = page.limit;
      this.getList();
    },
    removeDep(row) {
      this.$confirm(
        `此操作将永久删除【${row.department} - ${row.group}】部门小组信息, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          remove_by_id(row.id).then((response) => {
            if (response.data > 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            } else {
              this.$message({
                type: "info",
                message: "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    updateDep(row) {
      this.departmentEdit.show = true;
      this.departmentEdit.data = row;
    },
 
    addCtlShow(row) {
      this.departmentAdd.show = true;
    },
  },
};
</script>
